// Copyright 2020 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package templates

import (
  "bytes"
  "fmt"
  "io"
  "io/ioutil"
  "path"
  "strings"

  "gopkg.in/macaron.v1"

  "github.com/midoks/go-p2p-server/internal/tools"
)

//go:generate go-bindata -nomemcopy -nometadata -ignore="\\.DS_Store" -pkg=templates -prefix=../../../templates -debug=false -o=templates_gen.go ../../../templates/...

// fileSystem implements the macaron.TemplateFileSystem interface.
type fileSystem struct {
  files []macaron.TemplateFile
}

func (fs *fileSystem) ListFiles() []macaron.TemplateFile {
  return fs.files
}

func (fs *fileSystem) Get(name string) (io.Reader, error) {
  for i := range fs.files {
    if fs.files[i].Name()+fs.files[i].Ext() == name {
      return bytes.NewReader(fs.files[i].Data()), nil
    }
  }
  return nil, fmt.Errorf("file %q not found", name)
}

// NewTemplateFileSystem returns a macaron.TemplateFileSystem instance for embedded assets.
// The argument "dir" can be used to serve subset of embedded assets. Template file
// found under the "customDir" on disk has higher precedence over embedded assets.
func NewTemplateFileSystem(dir, customDir string) macaron.TemplateFileSystem {
  if dir != "" && !strings.HasSuffix(dir, "/") {
    dir += "/"
  }

  var files []macaron.TemplateFile
  names := AssetNames()
  for _, name := range names {
    if !strings.HasPrefix(name, dir) {
      continue
    }

    // Check if corresponding custom file exists
    var err error
    var data []byte
    fpath := path.Join(customDir, name)
    if tools.IsFile(fpath) {
      data, err = ioutil.ReadFile(fpath)
    } else {
      data, err = Asset(name)
    }
    if err != nil {
      panic(err)
    }

    name = strings.TrimPrefix(name, dir)
    ext := path.Ext(name)
    name = strings.TrimSuffix(name, ext)
    files = append(files, macaron.NewTplFile(name, data, ext))
  }
  return &fileSystem{files: files}
}
